package com.example.cp;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyCP extends ContentProvider {
    private MySqlite mySqlite;
    private SQLiteDatabase db;
    @Override
    public boolean onCreate() {
        //实例化数据库
        mySqlite = new MySqlite(getContext());
        db = mySqlite.getWritableDatabase();
        if(db!=null){
            return true;
        }else {
            return false;
        }
    }
    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        //新增数据
        db.insert("user",null,values);
        return uri;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        //删除数据  可以不要返回值  当有返回值时，1代表删除成功 0代表删除失败
        return db.delete("user",selection,selectionArgs);
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        //更新数据    可以不要返回值  当有返回值时，1代表删除成功 0代表删除失败
        return db.update("user",values,selection,selectionArgs);
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        //查询数据
        return db.query("user",projection,selection,selectionArgs,null,null,sortOrder);
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }


}
